package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.concurrent.CompletableFuture;


@Cmd(code = GameProtocol.S_TOMB_UNLOCK_DOLL,desc = "秦始皇陵解锁人偶")
public class TombUnlockPayDollCmd extends AbstractCommand {

    private Tomb.TombUnlockPayDollReq clientReqMsg;         //客户端发上来

    private Tomb.TombUnlockPayDollResp.Builder gameRespMsg;         //返回客户端


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        clientReqMsg = Tomb.TombUnlockPayDollReq.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.gameRespMsg = Tomb.TombUnlockPayDollResp.newBuilder();
        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (activityInfo == null) {
            this.gameRespMsg.setRet(GameErrorCode.E_ACTIVITY_NOT_OPEN);
            player.sendPacket(ClientProtocol.U_TOMB_UNLOCK_DOLL, this.gameRespMsg);
            return;
        }
        int ret = player.getModule(TombModule.class).isOpen(false);
        if (ret > 0) {
            this.gameRespMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_TOMB_UNLOCK_DOLL, this.gameRespMsg);
            return;
        }
        int payDoll = TombMgr.getConfig().getConfig().getTOMB_ALL_DOLL_ID_LIST().get(1);
        if (clientReqMsg.getDollId()!=payDoll){
            this.gameRespMsg.setRet(GameErrorCode.E_TOMB_DOLL_NO_PAY_TYPE);
            player.sendPacket(ClientProtocol.U_TOMB_UNLOCK_DOLL, this.gameRespMsg);
            return;
        }

        String costItem = TombMgr.getConfig().getConfig().getTOMB_NANNY_DOLL_COST();
        Property property = PropertyHelper.parseStringToProperty(costItem);
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(property)) { //物品不足
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            player.sendPacket(ClientProtocol.U_TOMB_UNLOCK_DOLL, this.gameRespMsg);
            return;
        }
        player.getModule(PreReduceModule.class).preReduce(property);

        CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToActivityCrossServerForUnionGroup(player.getUserId(), CrossProtocol.C_TOMB_CROSS_UNLOCK_PAY_DOLL, activityInfo.getActivityId(), this.clientReqMsg.toBuilder(), this.gameRespMsg);
        future.thenAccept(syncResponse -> {
            player.getModule(PreReduceModule.class).restorePreReduce(property);
            if (this.gameRespMsg.getRet()!=0){
                player.sendPacket(ClientProtocol.U_TOMB_UNLOCK_DOLL,this.gameRespMsg);
                return;
            }
            player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.Tomb,eLogMoneyType.TombPayDollUnlock);
            player.sendPacket(ClientProtocol.U_TOMB_UNLOCK_DOLL,this.gameRespMsg);
        });

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
